
package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.CaracterEspecialDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 
 * @author jsanchez
 */
public class CaracterEspecialDao extends DAO {

    public CaracterEspecialDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, CARACTER, REMPLAZARPOR, IDINFORME FROM CARACTERESPECIAL WHERE IDINFORME = ?");
            prep.setInt(1, id.intValue());
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new CaracterEspecialDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, CARACTER, REMPLAZARPOR, IDINFORME FROM CARACTERESPECIAL");
            for (resul = prep.executeQuery(); resul.next();) {
                c.add(getDatos(resul, new CaracterEspecialDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex, "No se pudo Cargar el Drive");
        } catch (SQLException ex) {
            throw new IGFormatoException(ex, "No se pudo cargar la lista de Caracteres Especiales");
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex, "No se pudo cargar la lista de Caracteres Especiales");
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        CaracterEspecialDTO dto = (CaracterEspecialDTO) obj;
        try {
            /*connectionIgFormato = */            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO CARACTERESPECIAL(CARACTER, REMPLAZARPOR, IDINFORME, ID) VALUES(?, ?, ?, ?)");
                dto.setId(getMaximoId("CARACTERESPECIAL"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE CARACTERESPECIAL SET CARACTER=?, REMPLAZARPOR=?, IDINFORME=? WHERE ID=?");
            }
            setDatos(prep, dto, "CARACTER, REMPLAZARPOR, IDINFORME, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM CARACTERESPECIAL WHERE IDINFORME = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex, "No se pudo eliminar el Caracter Especial.");
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex, "No se pudo eliminar el Caracter Especial.");
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM CARACTERESPECIAL WHERE CARACTER = ?");
            prep.setString(1, ((CaracterEspecialDTO) obj).getCaracter().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
